package com.ewzj.bigdata.mvp.presenter;

import android.app.Application;
import android.net.Uri;

import com.ewzj.bigdata.app.utils.Apputils;
import com.ewzj.bigdata.app.utils.RxUtils;
import com.ewzj.bigdata.app.utils.UriToPathUtil;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseData;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.SingleSource;
import io.reactivex.functions.Function;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.ewzj.bigdata.mvp.contract.FeedBackContract;
import com.jess.arms.utils.ArmsUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;


@ActivityScope
public class FeedBackPresenter extends BasePresenter<FeedBackContract.Model, FeedBackContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private ImageLoader mImageLoader;
    private AppManager mAppManager;

    @Inject
    public FeedBackPresenter(FeedBackContract.Model model, FeedBackContract.View rootView
            , RxErrorHandler handler, Application application
            , ImageLoader imageLoader, AppManager appManager) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mImageLoader = imageLoader;
        this.mAppManager = appManager;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void FeedBack(String content, List<Uri> paths) {
        Observable.fromIterable(paths).map(new Function<Uri, String>() {
            @Override
            public String apply(Uri uri) throws Exception {
                return UriToPathUtil.getImageAbsolutePath(mApplication, uri);
            }
        }).toList().flatMapObservable(new Function<List<String>, ObservableSource<BaseData<String>>>() {
            @Override
            public ObservableSource<BaseData<String>> apply(List<String> strings) throws Exception {
                return mModel.feedBack(content, strings);
            }
        }).compose(RxUtils.applySchedulersWithoutLoading(mRootView))
                .subscribe(new ErrorHandleSubscriber<BaseData<String>>(mErrorHandler) {
                    @Override
                    public void onNext(BaseData<String> stringBaseData) {
                        mRootView.feedSuccess();
                    }
                });
    }


}
